package com.frank.subject.application.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frank.common.entity.Result;
import com.frank.subject.application.convert.SubjectInfoDtoConverter;
import com.frank.subject.application.dto.SubjectInfoDto;
import com.frank.common.entity.PageInfo;
import com.frank.subject.domain.entity.SubjectInfoBo;
import com.frank.subject.domain.service.SubjectDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Climb Zou
 * @version v0.0.1
 * @date 2024/1/19 15:56
 * @description 刷题控制器
 */
@Slf4j
@RestController
@RequestMapping("/subject")
public class SubjectController {

    private final SubjectDomainService subjectDomainService;

    private final RocketMQTemplate rocketMQTemplate;

    public SubjectController(SubjectDomainService subjectDomainService,
                             RocketMQTemplate rocketMQTemplate) {
        this.subjectDomainService = subjectDomainService;
        this.rocketMQTemplate = rocketMQTemplate;
    }


    @PostMapping(value = "/add")
    public Result<Boolean> addSubject(@RequestBody SubjectInfoDto subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("Add subject, subjectInfoDTO: {}", subjectInfoDTO);
            }
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(), "Subject name is be empty!");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "Subject difficult is be empty!");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "Subject type is be empty!");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "Subject score is be empty!");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIdList()), "Subject category is be empty!");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIdList()), "Subject Label is be empty!");
            SubjectInfoBo subjectInfoBO = SubjectInfoDtoConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
            subjectDomainService.add(subjectInfoBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("Add subject error, Error: {}", e.getMessage(), e);
            return Result.fail();
        }
    }

    @GetMapping("/getSubjectPage")
    public Result<Page<SubjectInfoDto>> getSubjectPage(SubjectInfoDto subjectInfoDTO, PageInfo pageInfo) {
        try {
            if (log.isInfoEnabled()) {
                log.info("Get subject page, subjectInfoDTO: {}", subjectInfoDTO);
            }
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "Label id is be empty!");
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "Category id is be empty!");
            SubjectInfoBo subjectInfoBO = SubjectInfoDtoConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
           Page<SubjectInfoBo> subjectPage = subjectDomainService.getSubjectPage(subjectInfoBO, pageInfo);
            return Result.ok(SubjectInfoDtoConverter.INSTANCE.convertBoPageToDtoPage(subjectPage));
        } catch (Exception exception) {
            log.error("Get subject page error, Error: {}", exception.getMessage(), exception);
            return Result.fail();
        }
    }

    @GetMapping("/querySubjectInfo")
    public Result<SubjectInfoDto> querySubjectInfo(SubjectInfoDto subjectInfoDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("Get subject info, subjectInfoDTO: {}", subjectInfoDto);
            }
            Preconditions.checkNotNull(subjectInfoDto.getId(), "Subject id is be empty!");
            SubjectInfoBo subjectInfoBO = SubjectInfoDtoConverter.INSTANCE.convertDtoToBo(subjectInfoDto);
            SubjectInfoBo resultBo = subjectDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDto result = SubjectInfoDtoConverter.INSTANCE.convertBoToDto(resultBo);
            return Result.ok(result);
        } catch (Exception exception) {
            log.error("Get subject info error, Error: {}", exception.getMessage(), exception);
            return Result.fail();
        }
    }


    @GetMapping("/getContributeList")
    public Result<List<SubjectInfoDto>> getContributeList() {
        try {
            if (log.isInfoEnabled()) {
                log.info("Get contribute list");
            }
            List<SubjectInfoBo> boList = subjectDomainService.getContributeList();
            List<SubjectInfoDto> dtoList = SubjectInfoDtoConverter.INSTANCE.convertBoListToDtoList(boList);
            return Result.ok(dtoList);
        }catch (Exception exception) {
            log.error("Get contribute list error, Error: {}", exception.getMessage(), exception);
            return Result.fail();
        }
    }


    @GetMapping("/sync-es")
    public Result<Boolean>  syncEs() {
        try {
            if (log.isInfoEnabled()) {
                log.info("Sync es job started...");
            }
            return Result.ok(subjectDomainService.syncEs());
        }catch (Exception exception) {
            log.error("Sync es job error, Error: {}", exception.getMessage(), exception);
            return Result.fail();
        }
    }

    @GetMapping("/postMessage")
    public Result<Boolean> postMessage(String id) {
        rocketMQTemplate.convertAndSend("first-topic", "Frank，早上好"+id);
        return Result.ok(true);
    }

}
